package cn.johnyu.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

import util.JSONUtil;

public class WebServiceUtil {

	private static Properties prop = new Properties();

	public static List<String> adDish() {

		try {
			prop.load(new FileInputStream(
					WebServiceUtil.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e1) {
			System.out.println("propertire is not found");
			e1.printStackTrace();
		}
		String url = prop.getProperty("dish_url");
		Client client = Client.create();
		WebResource wr = null;
		wr = client.resource(url);
		String getData = wr.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		List<String> list = null;
		try {
			list = (List<String>) JSONUtil.json2Object(getData, List.class);
		} catch (IOException e) {
			System.out.println("webservice feedback to List wrong!");
		}
		return list;
	}

	public static List<String> backMerchant() {

		try {
			prop.load(new FileInputStream(
					WebServiceUtil.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e1) {
			System.out.println("properties is not found!");
			e1.printStackTrace();
		}
		String url = prop.getProperty("merchanter_url");
		Client client = Client.create();
		WebResource wr = null;
		wr = client.resource(url);
		String getData = wr.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		List<String> list = null;
		try {
			list = (List<String>) JSONUtil.json2Object(getData, List.class);
		} catch (IOException e) {
			System.out.println("webservice feedback to List wrong!");
		}
		return list;
	}

	public static Map<String, Integer> complainWebServiceUtil(String cid) {
		try {
			prop.load(new FileInputStream(
					WebServiceUtil.class.getClassLoader().getResource("webService.properties").getPath()));
		} catch (IOException e) {
			System.out.println("properties is not found");
			e.printStackTrace();
		}
		String url = prop.getProperty("conplain_url") + "/" + cid;
		Map<String, Integer> map = null;
		Client client = Client.create();
		WebResource wr = null;
		wr = client.resource(url);
		String get = wr.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		if (!"{}".equals(get)) {
			map = (Map<String, Integer>) JSONUtil.json2Map(get);
		}
		return map;
	}
}
